package com.smartstate.admin.biz.controller;

import com.smartstate.admin.biz.entity.vo.UserVo;
import com.smartstate.admin.biz.service.UserService;
import com.smartstate.admin.biz.service.impl.UserServiceImpl;
import com.smartstate.common.base.httpResult.ResultCode;
import com.smartstate.common.base.httpResult.ResultVO;
import com.smartstate.common.base.httpResult.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
@Api(tags = "对外暴露的接口")
@Slf4j
public class APIController extends BaseController {

    @Resource
    private UserService userService;
    
    @PostMapping(value = "/token")
    @ApiOperation(value = "获取token接口")
    public ResultVO getToken(@RequestParam String username, @RequestParam String password, @RequestParam(required = false) String code, @RequestParam(required = false) String randomStr){
        UserVo token = userService.loginByPassword(username, password, code, randomStr);
        if(token!=null){
            return success("获取token成功",token.getAccessToken());
        }
        return new ResultVO(ResultCode.INVALID_TOKEN);
    }

    @PostMapping(value = "/login")
    @ApiOperation(value = "登录接口")
    public ResultVO webLogin(@RequestParam String username, @RequestParam String password, @RequestParam(required = false) String code, @RequestParam(required = false) String randomStr){
        if(StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)){
            UserVo token = userService.loginByPassword(username, password,code,randomStr);
            if(null != token){
                return success(token);
            }
            return new ResultVO(ResultCode.USERNAME_ERROR);

        }
        return  ResultVO.error("参数缺失， 请输入参数");
    }
}
